﻿<script>
    var id = getUrlParam("ID");
    var userName =decodeURI(getUrlParam("Name"));
    var orgPost = "";
    var RoleList = {};
    var firstFlag = true;
    //update by ousihang 标识符
    var isInputChange = false;
    var addRoleListBox;

    $(function () {
        QueryFun(SetRoleListVal);

        $("#H3ToolBar").AspLinkToolBar();

        $("#hdSelectedRoleList").val(orgPost);
        $("#spUserName").text(userName);
        
        var gridManager = $("#UserRolelist").ligerGetGridManager();
        if (gridManager.rows.length <= 0){
            BindRoleList();
        }
        $("#iRoleSearch").on('input change', function () {
        	//update by ousihang
        	isInputChange = true;
            //查询用户角色列表
            BindRoleList();  
        })

        //$("#tdLeft").animate({ height: "300px" }, {
        //    step: function () {
        //        console.log($(this).height())
        //    }
        //}).resize(function () {
        //        console.log("tdLeft:" + $(this).height())
        //    })
        
    });
	function SetRoleListVal(){
		var gridManager = $("#UserRolelist").ligerGetGridManager();
		var SelectedRoles = "";
		//从查询的grid数据中获取需要选中的角色
		if(gridManager.rows.length>0){
			$.each(gridManager.rows,function(){
				SelectedRoles += ";" + this.RoleID;
			})
		}
		
		if(SelectedRoles.length == 0){SelectedRoles = "-1";} //默认不要选中
		
		$("#dvRoleList").ligerListBox({
			isShowCheckBox: true,isMultiSelect:true,
			data:RoleList,
			width:"100%",
			value:SelectedRoles
		})
	}
    function BindRoleList(){
        //角色列表
        var roleSourceUrl = $.Controller.ListOrgJob.GetRoleListSelect;
        var data = {};
        var keyword = $("#iRoleSearch").val();
        if (keyword) {
            roleSourceUrl = $.Controller.ListOrgJob.GetRoleListSelectByName;
            //update by ousihang
            //解决get方法传中文参数乱码
            var keyword4GetReq = encodeURI(keyword);
            data = { roleName: keyword4GetReq };
        }
		
        
        $.ajax({
            url: roleSourceUrl,
            type: 'get',
            dataType: "json",
            data: data,
            async: false,//同步执行
            success: function (result) {
                RoleList = result;//所有角色
                InitRoleList();
            }
        });
    }
    //角色下拉框初始化
    function InitRoleList() {
        var gridManager = $("#UserRolelist").ligerGetGridManager();
        var SelectedRoles = "";
        //从查询的grid数据中获取需要选中的角色
        if (gridManager.rows.length > 0)
        {
            $.each(gridManager.rows, function () {
                SelectedRoles += ";" + this.RoleID;
            })
        }
       
        if (SelectedRoles.length == 0) { SelectedRoles = "-1"; }//默认不要选中 
        //需要保存的数据
        orgPost = SelectedRoles;
        
        //update by ousihang 根据标识符分开处理
        if(isInputChange && addRoleListBox !== undefined){
        	addRoleListBox.clear();
        	addRoleListBox.clearContent();
        	if(RoleList.length){
        		addRoleListBox.setData(RoleList);
        	}
        	isInputChange = false;
        }else{
       		//强行居中input框
       		// debugger
       		$("#dvRoleList").addClass('innerInputMiddle');
        	addRoleListBox = $("#dvRoleList").ligerListBox({
                isShowCheckBox: true, 
                isMultiSelect: true,
                data: RoleList,
                value: SelectedRoles,
                width: "98%",
                height: "",
                onSelected: function (newvalue) {
                    orgPost = this.selectedValue;
                    if(this.selectedValue.indexOf(newvalue) > -1){
                    	SaveData();
                    }else{
                    	if(gridManager && gridManager.rows){
                    		$.each(gridManager.rows,function(){
                    			if(this.RoleID == newvalue){
                    				deleteOrgJob(newvalue, this.ObjectID);
                    			}
                    		})
                    	}
                    }
                },
                onSuccess: function () {
                }
            })
        }
    }

    //查询用户角色列表
    function QueryFun(fn) {
        var userid = getUrlParam("ID");
        CreateLigerGrid(
         $("#UserRolelist"),
         GetRoleViewColumns(),
         $.Controller.OrgUser.GetUserRoleList,
         false, "97%",
         null, null, fn,
         {
             userID: userid
         }
        );
    }

    //角色用户列表列
    function GetRoleViewColumns() {
        var primaryKey = $.Lang("ListOrgJob.PrimaryKey");
        var userAccount = $.Lang("ListOrgJob.UserAccount");
        var userName = $.Lang("ListOrgJob.UserName");
        var roleName = $.Lang("ListOrgJob.DisplayName");
        var managerScope = $.Lang("ListOrgJob.ManagerScope");
        var operation = $.Lang("ListOrgJob.Operation");

        var edit = $.Lang("GlobalButton.Edit");
        var del = $.Lang("GlobalButton.Delete");

        return [
            { display: primaryKey, name: "ObjectID", hide: true },
            { display: userAccount, name: "UserCode" },
            { display: userName, name: "UserName" },
            { display: roleName, name: "RoleName" },
            { display: managerScope, name: "ManagerScope" },
            {
                display: operation, name: '', render: function (row) {
                    return "<div style='float:left;'><a href='javascript:void(0)' onclick=\"EditRoleUser('" + row.RoleID + "','" + row.ObjectID + "')\">" + edit + "</div>";
                    //+ "<div style='padding-left:5px;float:left;'><a href='javascript:void(0)' onclick=\"deleteOrgJob('" + row.RoleID + "','" + row.ObjectID + "')\">" + del + "</a></div>"
                }
            }];
    }

    //删除用户角色
    function deleteOrgJob(roleid, id) {
        var ajaxUrl = $.Controller.ListOrgJob.DelRoleUser
        var deleteConfirm = $.Lang("msgGlobalString.AppMenuList_Msg0");

        $.ligerDialog.confirm(deleteConfirm, function (result) {
            // debugger;
            if (result) {
                $.ajax({
                    url: ajaxUrl,
                    data: { roleUserId: id, roleId: roleid },
                    success: function (result) {
                        QueryFun(null);
                        //debugger;
                        //下拉列表里的选择项删除
                        //var selectrolelist = $("#hdSelectedRoleList").val(); //已选择的角色列表
                        //selectrolelist = selectrolelist.replace(roleid, "").replace(";;",";");
                        //$("#hdSelectedRoleList").val(selectrolelist);
                        //InitRoleList(RoleList);
                        //刷新页面
                        var userid = getUrlParam("ID");
                        parent.InitRoleList(userid);

                    }
                });
            }else{
            	SetRoleListVal();
            }
        });
    }

    //编辑角色用户
    function EditRoleUser(roleid, roleuserid) {
        //debugger;
        var editOrgJob = $.Lang("ListOrgJob.RoleUserEidt");
        ShowDialog(editOrgJob, _PORTALROOT_GLOBAL + "/admin/TabMaster.html?url=Organization/EditRoleUser.html&roleid=" + roleid + "&id=" + roleuserid);
    }

    function SaveData() {
        // debugger;
        var ajaxUrl = $.Controller.OrgUser.SaveUserRoles
        $.ajax({
            url: ajaxUrl,
            data: { UserID: id, OrgPostIDS: orgPost },
            success: function (result) {
                // top.workTab.reload(top.workTab.getSelectedTabItemID());
                QueryFun(function(){});
                //刷新页面
                var userid = getUrlParam("ID");
                parent.InitRoleList(userid);
                if(result.Success){
                	$.H3Dialog.Success({content:$.Lang("msgGlobalString.SaveSucced")});
                }
            }
        });
    }

    function dropDownchange() {
        if ($(this).val() == "") {
            var manager = $(this).ligerGetComboBoxManager();
            $(manager.selectBox).find("input[type='checkbox']").each(function () {
                $(this).removeAttr("checked");
            });
            $(manager.selectBox.table).find(".l-checkbox-checked").each(function () {
                $(this).removeClass("l-checkbox-checked");
            });
        }
    }
</script>
<style>
    .l-listbox tr.l-selected td {
        background-color: #39f;
    }
    /*update by ousihang*/
    #dvRoleList td input{
		display: table-cell;
		margin-top: 3px;
	}
</style>
<!-- <div id="H3ToolBar">
    <a id="btnSubmit" href="javscript:void(0)"  data-icon="fa fa-save" onclick="SaveData()" data-lang="GlobalButton.Save"></a>
</div> -->
<div class="H3Panel">
    <table style="width:100%">
        <!--所属角色-->
        <tr >
            <td style="height:45px;" colspan="2">
                <table class="tableList" style="width:100%">
                    <tr>
                        <td style="width:20%;text-align:right">用户名</td>
                        <td style="width: 20%; text-align: center">
                            <span id="spUserName"></span>
                        </td>
                        <td style="width:20%;text-align:center"></td>
                        <td style="width:40%">
                            <!--<input type="text" id="selectRoleList"  />-->
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        
        <tr>
           
            <td valign="top" id="tdLeft">
                <table style="width:98%;height:100%;min-height:300px;word-break:break-all;word-wrap:break-word;">
                    <tr>
                        <td height="40px" style="padding-left:10px;">
                            <input id="iRoleSearch" type="text" style="width:98%" placeholder="搜索" />
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" style="padding-left:10px;">
                            <div id="dvRoleList" style="width:98%;min-height:360px;">

                            </div>
                        </td>
                    </tr>
                </table>
            </td>
            <td colspan="3" rowspan="2">
                <div id="UserRolelist"></div>
            </td>
        </tr>
        
    </table>
</div>